package com.summerbird.mallchat.common.websocket.service.adapter;

import cn.hutool.core.bean.BeanUtil;
import com.summerbird.mallchat.common.chat.domain.dto.ChatMessageMarkDTO;
import com.summerbird.mallchat.common.chat.domain.dto.ChatMsgRecallDTO;
import com.summerbird.mallchat.common.chat.domain.vo.response.ChatMessageResp;
import com.summerbird.mallchat.common.user.domain.entity.User;
import com.summerbird.mallchat.common.user.domain.enums.ChatActiveStatusEnum;
import com.summerbird.mallchat.common.websocket.domain.enums.WSRespTypeEnum;
import com.summerbird.mallchat.common.websocket.domain.vo.response.*;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.Date;

@Component
public class WebSocketAdapter {

    public static WSBaseResp<WSLoginUrl> buildLoginResp(WxMpQrCodeTicket wxMpQrCodeTicket) {
        WSBaseResp<WSLoginUrl> wsBaseResp = new WSBaseResp<>();
        wsBaseResp.setType(WSRespTypeEnum.LOGIN_URL.getType());
        wsBaseResp.setData(WSLoginUrl.builder().loginUrl(wxMpQrCodeTicket.getUrl()).build());
        return wsBaseResp;
    }

    public static WSBaseResp<?> buildLoginSuccessResp(User user, String token) {
        WSBaseResp<WSLoginSuccess> wsBaseResp = new WSBaseResp<>();
        wsBaseResp.setType(WSRespTypeEnum.LOGIN_SUCCESS.getType());
        WSLoginSuccess wsLoginSuccess = WSLoginSuccess.builder()
                .uid(user.getId())
                .name(user.getName())
                .avatar(user.getAvatar())
                .token(token)
                .build();
        wsBaseResp.setData(wsLoginSuccess);
        return wsBaseResp;
    }

    public static WSBaseResp<?> buildLoginScanSuccessResp() {
        WSBaseResp wsBaseResp = new WSBaseResp<>();
        wsBaseResp.setType(WSRespTypeEnum.LOGIN_SCAN_SUCCESS.getType());
        wsBaseResp.setData(null);
        return wsBaseResp;
    }

    public static WSBaseResp<?> buildInvalidDateTokenResp() {
        WSBaseResp wsBaseResp = new WSBaseResp<>();
        wsBaseResp.setType(WSRespTypeEnum.INVALIDATE_TOKEN.getType());
        wsBaseResp.setData(null);
        return wsBaseResp;
    }

    public static WSBaseResp<?> buildBlack(User user) {
        WSBaseResp<WSBlack> wsBaseResp = new WSBaseResp<>();
        wsBaseResp.setType(WSRespTypeEnum.BLACK.getType());
        WSBlack wsBlackUser = WSBlack.builder()
                .uid(user.getId())
                .build();
        wsBaseResp.setData(wsBlackUser);
        return wsBaseResp;
    }

    public static WSBaseResp<ChatMessageResp> buildMsgSend(ChatMessageResp msgResp) {
        WSBaseResp<ChatMessageResp> wsBaseResp = new WSBaseResp<>();
        wsBaseResp.setData(msgResp);
        wsBaseResp.setType(WSRespTypeEnum.MESSAGE.getType());
        return wsBaseResp;
    }

    public static WSBaseResp<WSMsgMark> buildMsgMarkSend(ChatMessageMarkDTO dto, Integer markCount) {
        WSMsgMark.WSMsgMarkItem item = new WSMsgMark.WSMsgMarkItem();
        BeanUtil.copyProperties(dto, item);
        item.setMarkCount(markCount);
        WSBaseResp<WSMsgMark> wsBaseResp = new WSBaseResp<>();
        wsBaseResp.setType(WSRespTypeEnum.MARK.getType());
        WSMsgMark mark = new WSMsgMark();
        mark.setMarkList(Collections.singletonList(item));
        wsBaseResp.setData(mark);
        return wsBaseResp;
    }

    public static WSBaseResp<WSMsgRecall> buildMsgRecall(ChatMsgRecallDTO dto) {
        WSMsgRecall wsMsgRecall = new WSMsgRecall();
        BeanUtil.copyProperties(dto, wsMsgRecall);
        WSBaseResp<WSMsgRecall> wsBaseResp = new WSBaseResp<>();
        wsBaseResp.setType(WSRespTypeEnum.RECALL.getType());
        wsBaseResp.setData(wsMsgRecall);
        return wsBaseResp;
    }

    public static WSBaseResp<WSMemberChange> buildMemberRemoveWS(Long roomId, Long uid) {
        WSBaseResp<WSMemberChange> wsBaseResp = new WSBaseResp<>();
        wsBaseResp.setType(WSRespTypeEnum.MEMBER_CHANGE.getType());
        WSMemberChange wsMemberChange = new WSMemberChange();
        wsMemberChange.setChangeType(WSMemberChange.CHANGE_TYPE_REMOVE);
        wsMemberChange.setUid(uid);
        wsMemberChange.setRoomId(roomId);
        wsBaseResp.setData(wsMemberChange);
        return wsBaseResp;
    }

    public static WSBaseResp<WSOnlineOfflineNotify> buildOfflineNotifyResp(User user) {
        WSBaseResp<WSOnlineOfflineNotify> wsBaseResp = new WSBaseResp<>();
        wsBaseResp.setType(WSRespTypeEnum.ONLINE_OFFLINE_NOTIFY.getType());
        WSOnlineOfflineNotify wsOnlineOfflineNotify = new WSOnlineOfflineNotify();
        wsOnlineOfflineNotify.setChangeList(Collections.singletonList(buildOfflineInfo(user)));
        wsBaseResp.setData(wsOnlineOfflineNotify);
        return wsBaseResp;
    }

    private static ChatMemberResp buildOfflineInfo(User user) {
        ChatMemberResp resp = new ChatMemberResp();
        BeanUtil.copyProperties(user, resp);
        resp.setUid(user.getId());
        resp.setActiveStatus(ChatActiveStatusEnum.OFFLINE.getStatus());
        resp.setLastOptTime(new Date());
        return resp;
    }

    public static WSBaseResp<?> buildApplySend(WSFriendApply wsFriendApply) {
        WSBaseResp<WSFriendApply> wsBaseResp = new WSBaseResp<>();
        wsBaseResp.setData(wsFriendApply);
        wsBaseResp.setType(WSRespTypeEnum.APPLY.getType());
        return wsBaseResp;
    }
}
